from django.forms import modelformset_factory
from django.shortcuts import render
from django.urls import re_path

from app01 import models
from stark.service.stark import StarkHandler, StarkModelForm


class StudyRecordModelForm(StarkModelForm):
    class Meta:
        model = models.StudyRecord
        fields = '__all__'


class StudyRecordConfig(StarkHandler):

    def get_urls(self):
        patterns = [
            re_path(r'^list/$', self.wrapper(self.changelist_view), name=self.get_list_url_name),
        ]
        return patterns

    def changelist_view(self, request):
        cid = self.request.GET.get('cid')
        modelformset_cls = modelformset_factory(models.StudyRecord,StudyRecordModelForm,extra=5)
        formset = modelformset_cls(queryset=models.StudyRecord.objects.filter(course_record_id=cid))
        return render(request,'study_record.html',{'formset':formset})
